package cn.s1995.sloth.app.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.entity.Shop;
import cn.s1995.sloth.bizs.entity.UserFollow;
import cn.s1995.sloth.bizs.oo.vo.ShopDetailVO;
import cn.s1995.sloth.bizs.service.ShopService;
import cn.s1995.sloth.bizs.service.UserFollowService;
import cn.s1995.sloth.bizs.service.common.CacheService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 工厂
 */
@RestController
@RequestMapping("/m/shop")
@RequiredArgsConstructor
@Tag(description = "Shop", name = "APP-工厂模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiShopController {

	private final ShopService shopService;

	private final UserFollowService userFollowService;

	private final CacheService cacheService;

	@Resource
	private HttpServletRequest request;

	@Inner(false)
	@ApiOperationSupport(order = 1)
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<ShopDetailVO> getById(@PathVariable("id" ) Long id) {
		ShopDetailVO vo = shopService.getShopDetail(id);
    	//回显关注状态
		Long uid = cacheService.getUidByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
		if(ObjectUtil.isNotNull(uid) && ObjectUtil.isNotNull(vo)) {
			long count = userFollowService.count(
				Wrappers.lambdaQuery(UserFollow.class)
						.eq(UserFollow::getType, BizCst.UserFollow.Type.shop)
						.eq(UserFollow::getBindId, id)
						.eq(UserFollow::getUid, uid)
			);
			if(count > 0)
				vo.setFollowFlag(true);
		}
        return R.ok(vo);
    }

	@ApiOperationSupport(order = 2)
    @Operation(summary = "关注店铺", description = "关注店铺")
	@Parameters({
	   @Parameter(name = "id",description = "工厂id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/{id}/follow")
	public R follow(@PathVariable("id") Long id) {
		return shopService.follow(id);
	}

	@Inner(false)
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R<Page<ShopDetailVO>> searchPage(@ParameterObject Page page, @ParameterObject Shop req) {
        return R.ok(shopService.selectShopPage(page, req));
	}
}
